/*!
 *	@file		app_main.c
 *	@author		T4056 Student
 *	@date		2024-07-13
 *	@version	V1.0
 *	@par		CopyRight(c)
 *				2024 T4056 Student.All rights reserved.
 */
#include "app_main.h"

static TaskHandle_t LED_TaskHandle = NULL;	/**< LED Task Handler */
static TaskHandle_t ELOG_TaskHandle = NULL;	/**< ELOG Task Handler */
static void start_task(void * PvParameters);
static void led_task(void * PvParameters);
static void elog_task(void * PvParameters);

#ifdef LOG_TAG
#undef LOG_TAG
#define LOG_TAG          "APP_MAIN_TAG"
#endif

/*!
 *	@details	1.Create Start Task 2.Start Scheduler
 */
void app_main(void)
{
	xTaskCreate(	start_task,
					"start_task",
					START_TASK_SIZE,
					NULL,
					START_TASK_PRIO,
					NULL);
	
	vTaskStartScheduler();
	while(1)
	{

	}
}

/*!
 *	@brief		Start Task
 *	@details	1.Enter Critical 2.Do what ever you want 3.Exit Critical 4.Delete Task Self
 *	@param		PvParameters：Never Use
 *	@return		None
 */
static void start_task(void * PvParameters)
{
	(void )PvParameters;
	portENTER_CRITICAL();
	xTaskCreate(	led_task,
					"led_task",
					LED_TASK_SIZE,
					NULL,
					LED_TASK_PRIO,
					&LED_TaskHandle);
	
	xTaskCreate(	elog_task,
					"elog_task",
					512,
					NULL,
					10,
					&ELOG_TaskHandle);
	
	portEXIT_CRITICAL();
	vTaskDelete(NULL);
}


/*!
 *	@brief		LED Task
 *	@details	Periodically turn on/off the LED
 *	@param		PvParameters：Never Use
 *	@return		None
 */
static void led_task(void * PvParameters)
{
	(void )PvParameters;

	static GPIO_PinState led_status = GPIO_PIN_RESET;

	while (1)
	{
		vTaskDelay(pdMS_TO_TICKS(100));
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,led_status);
		if(led_status == GPIO_PIN_SET)
			led_status = GPIO_PIN_RESET;
		else
			led_status = GPIO_PIN_SET;
	}
}

static void elog_task(void * PvParameters)
{
	(void )PvParameters;
	
	SEGGER_RTT_Init();
	#if 0
	int a = 3;
	SEGGER_RTT_TerminalOut(0,RTT_CTRL_BG_BLACK""RTT_CTRL_TEXT_BRIGHT_GREEN"SEGGER_RTT_TerminalOut 0\r\n");
	SEGGER_RTT_TerminalOut(1,RTT_CTRL_BG_BLUE""RTT_CTRL_TEXT_BRIGHT_YELLOW"SEGGER_RTT_TerminalOut 1\r\n");
	SEGGER_RTT_SetTerminal(2);
	SEGGER_RTT_printf(0,RTT_CTRL_BG_WHITE""RTT_CTRL_TEXT_BRIGHT_BLACK"SEGGER_RTT_printf 2\r\n");
	SEGGER_RTT_SetTerminal(3);
	SEGGER_RTT_printf(0,"SEGGER_RTT_printf %d\r\n", a);
	#endif
	SEGGER_RTT_SetTerminal(0);
	SEGGER_RTT_printf(0,"Hello World\r\n");
	
	elog_init();
	elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL & ~(ELOG_FMT_P_INFO | ELOG_FMT_TAG | ELOG_FMT_LVL | ELOG_FMT_FUNC | ELOG_FMT_LINE | ELOG_FMT_DIR | ELOG_FMT_T_INFO));
	elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
	elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
	elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
	elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_P_INFO));
	elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_P_INFO));
	elog_start();
	
	log_a("This is Easy_Logger Assert Testting!\r\n");
	log_e("This is Easy_Logger Error Testting!\r\n");
	log_w("This is Easy_Logger Warning Testting!\r\n");
	log_i("This is Easy_Logger Infofmation Testting!\r\n");
	log_d("This is Easy_Logger Debug Testting!\r\n");
	log_v("This is Easy_Logger Verbose Testting!\r\n");
	
	while(1)
	{
//		log_a("ELOG Output\r\n");
		vTaskDelay(pdMS_TO_TICKS(1000));
	}
}
